home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 …ember: Reference Library / Dev.CD Dec 94.toast / Periodicals / develop / develop Issue 15 / develop 15 code / Floating Windows / Sample Source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-13  |  5.2 KB  |  193 lines  |  [TEXT/MMCC]

  1. /*    Application skeleton by Dean Yu    */
  2. /*    Main routine and initialization routines                    */
  3.  
  4. // If the interfaces aren't included yet, do the standard includes. Otherwise, precompiled headers
  5. // have been loaded already, thank you very much
  6. #ifndef __TYPES__
  7.     #include <Types.h>
  8.     #include <AppleEvents.h>
  9.     #include <Desk.h>
  10.     #include <Dialogs.h>
  11.     #include <Editions.h>
  12.     #include <Events.h>
  13.     #include <Fonts.h>
  14.     #include <Menus.h>
  15.     #include <SegLoad.h>
  16.     #include <TextUtils.h>
  17.     #include <ToolUtils.h>
  18.     #include <Windows.h>
  19. #endif
  20.  
  21. // Not in Metrowerks default precompiled headers
  22. #include <GestaltEqu.h>
  23.  
  24. #include "FloaterSample.h"
  25. #include "appleEventHandlers.h"
  26. #include "eventHandler.h"
  27. #include "menuDispatch.h"
  28. #include "trapAvail.h"
  29.  
  30. /*    Private routine prototypes */
  31.  
  32. static void InitStuff(void);
  33. static void SetUpMenus(void);
  34. static void SetUpWindows(void);
  35. static void SetupRecordingFloater(void);
  36. static void SetupToolsFloater(void);
  37. static void SetUpLimits(void);
  38. static void SetUpAppleEvents(void);
  39.  
  40. /*    Global variables    */
  41.  
  42. Boolean    gDone;                            /* Set to true if user selects Quit */
  43. Boolean gInBackground;                    /* Set to true if app is switched into background */
  44. short    gTimeToDialogDismissal;
  45. Rect    gDragArea;                        /* Area in which a window can be dragged */
  46. Rect    gGrowBounds;                    /* Min and maximum size a window can be sized to */
  47.  
  48. WindowRef    gRecordingFloater;            // Reference to recording floating window
  49. WindowRef    gToolsFloater;                // Reference to tools floating window
  50.  
  51. ActivateHandlerUPP    gRecordingFloaterActivateHandler; // The UPPs for my windows
  52. ActivateHandlerUPP    gToolsFloaterActivateHandler;
  53. ActivateHandlerUPP    gActivateEventHandler;
  54.  
  55.  
  56. void main()
  57. {
  58.     MaxApplZone();
  59.  
  60.     InitStuff();
  61.     EventLoop();
  62. }
  63.  
  64. void InitStuff()
  65. {
  66.     EventRecord    firstEvent;
  67.     OSErr        gestaltError;
  68.     long        gestaltResponse;
  69.     
  70.     InitGraf((Ptr) &qd.thePort);
  71.     InitFonts();
  72.     InitWindows();
  73.     InitMenus();
  74.     InitDialogs(nil);
  75.     InitCursor();
  76.     
  77.     if (!TrapAvailable(_Gestalt))                                            /* Look for _Gestalt */
  78.         FatalErrorAlert(kSystemVersionTooOld);
  79.     
  80.     WaitNextEvent(everyEvent, &firstEvent, 60, nil);
  81.     FlushEvents(everyEvent, 0);
  82.     
  83.     /*    Check to see if certain features are implemented */
  84.     
  85.     gestaltError = Gestalt(gestaltAppleEventsAttr, &gestaltResponse);        /* Check for AppleEvents™ */
  86.     if (!gestaltError)
  87.         {
  88.             if ((gestaltResponse & (1 << gestaltAppleEventsPresent)) == 0)
  89.                 FatalErrorAlert(kNoAppleEvents);
  90.         }
  91.     else
  92.         FatalErrorAlert(kGestaltError);
  93.     
  94.     gestaltError = Gestalt(gestaltPPCToolboxAttr, &gestaltResponse);        /* Check for PPC Toolbox */
  95.     if (gestaltError)
  96.         FatalErrorAlert(kGestaltError);
  97.     
  98.     SetUpMenus();
  99.     SetUpWindows();
  100.     SetUpLimits();
  101.     SetUpAppleEvents();
  102. }
  103.  
  104.  
  105. /*    Get the menu bar from resource fork.  Fill  menu with DAs.  Draw it */
  106.  
  107. void SetUpMenus()
  108. {
  109.     Handle    theMenu;
  110.     
  111.     theMenu = GetNewMBar(rMbarID);
  112.     SetMenuBar(theMenu);
  113.     DisposHandle(theMenu);
  114.     AddResMenu(GetMHandle(rAppleMenuID),'DRVR');
  115.     DisableItem(GetMHandle(rEditMenuID), 0);
  116.     CheckItem(GetMHandle(rFloatersMenuID), kRecordingItem, true);
  117.     CheckItem(GetMHandle(rFloatersMenuID), kToolsItem, true);
  118.     DrawMenuBar();
  119. }
  120.  
  121. /*
  122.     Set up any windows needed by the application
  123. */
  124.  
  125. void SetUpWindows()
  126. {
  127.     SetupRecordingFloater();
  128.     SetupToolsFloater();
  129.     
  130.     gActivateEventHandler = NewActivateHandlerProc(&ActivateEventHandler);
  131. }
  132.  
  133. void SetupRecordingFloater()
  134. {
  135.     Rect        theRect;
  136.     PicHandle    recordingControlsPicture;
  137.     OSErr        error;
  138.     
  139.     recordingControlsPicture = (PicHandle) GetPicture(128);
  140.     theRect = (**recordingControlsPicture).picFrame;
  141.     OffsetRect(&theRect, 70, 70);
  142.     
  143.     gRecordingFloaterActivateHandler = NewActivateHandlerProc(&RecordingFloaterActivateHandler);
  144.     error = NewWindowReference(&gRecordingFloater, &theRect, "\pRecord", true, kHasPaletteTitlebarMask,
  145.                                 (WindowRef) -1, 0, gRecordingFloaterActivateHandler);
  146.     SetWindowPic((WindowPtr) gRecordingFloater, recordingControlsPicture);
  147. }
  148.  
  149. void SetupToolsFloater()
  150. {
  151.     Rect        theRect;
  152.     PicHandle    toolsPicture;
  153.     OSErr        error;
  154.     
  155.     toolsPicture = (PicHandle) GetPicture(130);
  156.     theRect = (**toolsPicture).picFrame;
  157.     OffsetRect(&theRect, 170, 100);
  158.     
  159.     gToolsFloaterActivateHandler = NewActivateHandlerProc(&ToolsFloaterActivateHandler);
  160.     error = NewWindowReference(&gToolsFloater, &theRect, "\pTools", true, kHasPaletteTitlebarMask,
  161.                                 (WindowRef) -1, 0, gToolsFloaterActivateHandler);
  162.     SetWindowPic((WindowPtr) gToolsFloater, toolsPicture);
  163. }
  164.  
  165. /*
  166.     Set up areas for the screen in which windows can be dragged and sized.
  167. */
  168.  
  169. void SetUpLimits()
  170. {
  171.     RgnHandle    deskTop;
  172.     
  173.     deskTop = GetGrayRgn();
  174.     gDragArea = (**deskTop).rgnBBox;
  175.     SetRect(&gGrowBounds, 48, 48,
  176.             ((gDragArea.right) - (gDragArea.left)),((gDragArea.bottom) - (gDragArea.top)));
  177. }
  178.  
  179. /*    Set up the AppleEvent dispatch table */
  180.  
  181. void SetUpAppleEvents()
  182. {
  183.     OSErr    installAppleEventError;
  184.     
  185.     installAppleEventError = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, (AEEventHandlerUPP) &OAPPHandler, 0, false);
  186.     installAppleEventError = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, (AEEventHandlerUPP) &ODOCHandler, 0, false);
  187.     installAppleEventError = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, (AEEventHandlerUPP) &PDOCHandler, 0, false);
  188.     installAppleEventError = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, (AEEventHandlerUPP) &QUITHandler, 0, false);
  189.  
  190.     if (installAppleEventError)
  191.         FatalErrorAlert(kAEInstallError);
  192. }
  193.